/*
 ============================================================================
 Name		: FileManagerUtil.cpp
 Author	  : JohnLZeng
 Version	 : 1.0
 Copyright   : Your copyright notice
 Description : CFileManagerUtil implementation
 ============================================================================
 */

#include "FileManagerUtil.h"

#include <f32file.h>
#include <COEMAIN.H>

FileManagerUtil::FileManagerUtil()
	{
	// No implementation required
	}

FileManagerUtil::~FileManagerUtil()
	{
	}

FileManagerUtil* FileManagerUtil::NewLC()
	{
	FileManagerUtil* self = new (ELeave) FileManagerUtil();
	CleanupStack::PushL(self);
	self->ConstructL();
	return self;
	}

FileManagerUtil* FileManagerUtil::NewL()
	{
	FileManagerUtil* self = FileManagerUtil::NewLC();
	CleanupStack::Pop(); // self;
	return self;
	}

void FileManagerUtil::ConstructL()
	{

	}

void FileManagerUtil::ScanForPostfix(const TDesC& aPath,
		const TDesC& aPostfix, CDesCArray& aArray)
	{
	TFileName path;
	path.Append(aPath);
	if (aPath.LocateReverse('\\') != aPath.Length()-1)
		path.Append('\\');
	
	TBuf<16> postfix;
	postfix.Append(aPostfix);
	if (postfix[0] != '.')
		postfix.Insert(0,_L("."));
	postfix.LowerCase();
	
	CDirScan* ds = CDirScan::NewLC(CCoeEnv::Static()->FsSession());
	ds->SetScanDataL(path,KEntryAttNormal, ESortNone);

	CDir* dirs;
	dirs = NULL;
	ds->NextL(dirs);
	while (dirs != NULL)
		{
		for (TInt i = 0; i < dirs->Count(); i++)
			{
			TParse fullentry;
			fullentry.Set((*dirs)[i].iName, &ds->FullPath(), NULL);
			
			TBuf<16> ext;
			ext.Append(fullentry.Ext());
			ext.LowerCase();
			
			if (ext == postfix)
				aArray.AppendL(fullentry.FullName());
			}
		delete dirs;
		dirs = NULL;
		ds->NextL(dirs);
		}

	CleanupStack::PopAndDestroy(ds);
	}
